class Drag
static var target;
static var range;
static var doDrag;
static var useSlide;
static var bounceSize;
static var isStartTrigger=false;
static var fromPos;
static var lastPos;
static var offsets;
static var speed;
static var isPressMoving;
public static function start(target,range,doDrag,useSlide,bounceSize){
	Me.startTrigger();
	Me.target=target;
	Me.range=range||[0,0,0,G.h-target.h];
	Me.doDrag=doDrag;
	Me.useSlide=useSlide;
	Me.bounceSize=bounceSize;
	Me.fromPos={x:target.x,y:target.y};
	Me.lastPos={x:target.x,y:target.y};
	Me.offsets=[];
	Me.isPressMoving=true;
}
public static function stop(){
	if(!Me.target) return;
	Me.target.setPos(Me.fromPos.x,Me.fromPos.y);
	Me.target=null;
}
static function pressMove(){
	if(!G.isPress){
		Me.isPressMoving=false;
		Me.startSlide();
		return;
	}
	Me.target.setPos(Me.fromPos.x+(G.x-G.pressX),Me.fromPos.y+(G.y-G.pressY));
	Me.checkRange();
	Me.offsets.push({x:Me.target.x-Me.lastPos.x,y:Me.target.y-Me.lastPos.y});
	if(Me.offsets.length>6) Me.offsets.shift();
	Me.lastPos.x=Me.target.x;
	Me.lastPos.y=Me.target.y;
}
static function startSlide(){
	if(!Me.useSlide||Me.offsets.length==0){
		Me.finish();
		return;
	}
	Me.speed={x:0,y:0};
	for(var i=0;i<Me.offsets.length;i++){
		var p=Me.offsets[i];
		Me.speed.x+=p.x;
		Me.speed.y+=p.y;
	}
	Me.speed.x=2*Me.speed.x/Me.offsets.length;
	Me.speed.y=2*Me.speed.y/Me.offsets.length;
}
static function slideMove(){
	Me.speed.x*=0.92;
	Me.speed.y*=0.92;
	if(Math.abs(Me.speed.x)<1) Me.speed.x=0;
	if(Math.abs(Me.speed.y)<1) Me.speed.y=0;
	Me.target.setPos(Me.target.x+Me.speed.x,Me.target.y+Me.speed.y);
	Me.checkRange();
	if(Me.lastPos.x==Me.target.x&&Me.lastPos.y==Me.target.y){
		Me.finish();
		return;
	}
	Me.lastPos.x=Me.target.x;
	Me.lastPos.y=Me.target.y;
}
static function checkRange(){
	var x=Math.min(Math.max(Me.target.x,Me.range[0]),Me.range[0]+Me.range[2]);
	var y=Math.min(Math.max(Me.target.y,Me.range[1]),Me.range[1]+Me.range[3]);
	Me.target.setPos(x,y);
	call(Me.doDrag,false);
}
static function finish(){
	call(Me.doDrag,true);
	Me.target=null;
}
static function startTrigger(){
	if(Me.isStartTrigger) return;
	Me.isStartTrigger=true;
	G.addFrameCall([Me.onFrame]);
}
static function onFrame(){
	var now=G.now;
	if(Me.target){
		if(Me.isPressMoving){
			Me.pressMove();
		}
		else{
			Me.slideMove();
		}
	}
}